package com.why.rpc.core.server.handler;


import com.why.rpc.core.common.enums.ErrorEnum;
import com.why.rpc.core.common.exception.RpcException;
import com.why.rpc.core.common.message.RpcRequest;
import com.why.simpleboot.core.common.utils.SimpleReflectUtils;

import java.lang.reflect.Method;


public class RequestHandler {
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RequestHandler.class);

    public static Object handler(Object service, RpcRequest request) {
        Object result = null;
        try {
            log.info("requesthandler开始工作....");
            Method method = service.getClass().getMethod(request.getMethodName(), request.getParameterTypes());
            if (method == null) {
                throw new RpcException(ErrorEnum.NOT_FOUND);
            }
            result = SimpleReflectUtils.invokeMethod(service, method, request.getMethodParameters());

        } catch (Exception e) {
            log.error("RequestHandler在执行对应方法的时候发生了错误");
            e.printStackTrace();
        }
        return result;
    }


}